﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ImmovablesManager
{
    /// <summary>
    /// Interaction logic for AdvertisementLog.xaml
    /// </summary>
    public partial class UserEntranceLog : Window
    {
        ObservableCollection<DataGridUserEntrancerLogDataView> dataGridUserEntranceLogDataViews = new ObservableCollection<DataGridUserEntrancerLogDataView>();
        public UserEntranceLog()
        {
            InitializeComponent();
            RefreshAdvLog();
        }

        private void RefreshAdvLog()
        {
           
            foreach (var story in AppController.ImmoRepository.GetAllUserEntrance())
            {
                string timeExit = "", duration = "";
                if (story.UserEntranceTimeEnter != null)
                {
                    string timeEnter =
                        ((DateTime)story.UserEntranceTimeEnter).ToString(
                            System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"));
                    if (story.UserEntranceTimeExit != null)
                    {
                        timeExit =
                            ((DateTime)story.UserEntranceTimeExit).ToString(
                                System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"));
                        duration = ( story.UserEntranceTimeExit - story.UserEntranceTimeEnter).Value.Duration().ToString("hh':'mm':'ss");
                    }

                    User user = AppController.ImmoRepository.GetUserById(story.UserEntranceUserId);
                    dataGridUserEntranceLogDataViews.Add(new DataGridUserEntrancerLogDataView()
                                                             {
                                                                 AdvId = story.UserEntranceId,
                                                                 Duration = duration,
                                                                 FIO =
                                                                     user.UserFirstName + " " + user.UserLastName +" " +
                                                                     user.UserThirdName,
                                                                 InputTime = timeEnter,
                                                                 OutputTime = timeExit,
                                                                 Role = user.UserRole

                                                             });

                }
            }

            //var resultList =  from item in dataGridUserEntranceLogDataViews
            //                                   orderby item.OutputTime
            //                                   select item;
           // dataGridUserEntranceLogDataViews.ToList().Sort(Compare1);
            DataGridUserEntrance.ItemsSource = dataGridUserEntranceLogDataViews;
        }

        private void DataGridLog_LoadingRow(object sender, DataGridRowEventArgs e)
        {
             DataGridUserEntrancerLogDataView data = (DataGridUserEntrancerLogDataView)e.Row.DataContext;

            if (data.Role == "director")
            {
                e.Row.Background = Brushes.LightCoral;
            }

            if (data.Role == "controller")
            {
                e.Row.Background = Brushes.LightSkyBlue;
            }
        }

        static int Compare1(DataGridUserEntrancerLogDataView a, DataGridUserEntrancerLogDataView b)
        {

            return a.OutputTime.CompareTo(b.OutputTime);

        }



 
    }
}
